package com.nt.train.service.impl;
import com.nt.train.dto.OrderInfo;
import com.nt.train.service.OrderService;
import com.nt.train.service.OrderStrategy;
import com.nt.train.service.context.HandlerOrderContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
/**
 * 订单-实现类
 * @author hlf
 * @version 1.0
 * @date 2021/12/20 11:02
 */
@Service
public class OrderServiceImpl implements OrderService {

    private static final Logger logger = LoggerFactory.getLogger(OrderServiceImpl.class);

    @Override
    public String handleOrder(OrderInfo orderInfo) {
        try {
            // 根据订单类型获取对应的创建订单策略
            OrderStrategy orderStrategy = HandlerOrderContext.getInstance(orderInfo.getOrderType());
            String userInfo = orderStrategy.getUserInfo(orderInfo.getBuyer());
            String goodsInfo = orderStrategy.getGoodsInfo(orderInfo.getGoodsName());
            Double discount = orderStrategy.getDiscount();
            String msg = "\n购买人信息：" + userInfo +
                    "\n商品信息：" + goodsInfo +
                    "\n优惠金额：" + discount +
                    "\n原价：" + orderInfo.getPrice() +
                    "\n优惠后金额：" + (orderInfo.getPrice() - discount);
            logger.info(msg);
            return msg;
        } catch (Exception e) {
            return e.getMessage();
        }
    }
}
